import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:wan_android/pages/page_search.dart';

class PageHot extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState

    return PageHotState();
  }
}

class PageHotState extends State<PageHot> {
  String hotApi = 'http://www.wanandroid.com/hotkey/json';
  List<Widget> hotList = []; //大家都在搜 list
  String normalApi = 'http://www.wanandroid.com/friend/json';
  List<Widget> normalList = []; //常用网站 list

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getDataList();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(left: 10.0,top: 10.0,bottom: 10.0),
          child: Text(
            '大家都在搜',
            style: TextStyle(fontSize: 16.0, color: Colors.red),
          ),
        ),
        Wrap(
          spacing: 6.0,
          runSpacing: 6.0,
          children: hotList,
        ),
        Padding(
          padding: EdgeInsets.only(left: 10.0,top: 10.0,bottom: 10.0),
          child: Text(
            '常用网站',
            style: TextStyle(fontSize: 16.0, color: Colors.red,),
          ),
        ),
        Wrap(
          spacing: 6.0,
          runSpacing: 6.0,
          children: normalList,
        )
      ],
    );
  }

  void _getDataList() async {
    Dio dio = Dio();
    await dio.get(hotApi).then((value) {
      print('hotApi: ');
      print(value.data);
      var data = value.data['data'];
      for (var value1 in data) {
        String name = value1['name'];
        hotList.add(ActionChip(
            backgroundColor: Theme.of(context).accentColor,
            label: Text(name),
            onPressed: () {
              _handleClickMethod(name);
            }));
      }
      setState(() {});
    }).catchError((error) {
      print(error);
    });

    await dio.get(normalApi).then((value) {
      print('normalApi: ');
      print(value.data);
      var data = value.data['data'];
      for (var value1 in data) {
        String name = value1['name'];
        normalList.add(ActionChip(
            backgroundColor: Theme.of(context).accentColor,
            label: Text(name),
            onPressed: () {
              _handleClickMethod(name);
            }));
      }
      setState(() {});
    }).catchError((error) {
      print(error);
    });
  }

  _handleClickMethod(String data){
    Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){
      return PageSearch(editStr: data,);
    }));
  }
}
